package cn.com.pet.administrator.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.pet.common.base.BaseAction;
import cn.com.pet.common.base.ErrorMessage;
import cn.com.pet.common.entity.AdminUser;
import cn.com.pet.common.service.AdminUserService;
import cn.com.pet.common.util.StringUtil;

@Namespace("/action")
@ParentPackage("base")
@SuppressWarnings({ "serial" })
public class AdminUserAction extends BaseAction {
	
	@Autowired
	AdminUserService adminUserService;

	@Action(value = "adminUserList", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/adminUserList.jsp") })
	public String getAdminUserList() {
		
		long id=StringUtil.parsLong(this.getRequest().getParameter("id"));
		String name=this.getRequest().getParameter("name");
		int status=StringUtil.parsInt(this.getRequest().getParameter("status"));
		
		AdminUser adminUser = new AdminUser();
		adminUser.setAdminUserId(id);
		adminUser.setAdminName(name);
		adminUser.setStatus(status);

		List<AdminUser> result = adminUserService.getAdminUserList(adminUser);
		
		this.getRequest().setAttribute("adminUserList", result);
		
		return SUCCESS;
	}

	@Action(value = "pageUpdateAdminUser", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/updateAdminUser.jsp") })
	public String pageUpdateAdminUser() {
		
		long id = StringUtil.parsLong(this.getRequest().getParameter("id"));

		AdminUser adminUser = new AdminUser();
		adminUser.setAdminUserId(id);

		AdminUser result = adminUserService.getAdminUserById(adminUser);
		
		this.getRequest().setAttribute("adminUser", result);
		
		return SUCCESS;
	}

	@Action(value = "adminUserUpdate", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/adminUserList.jsp") })
	public String adminUserUpdate() {
		String name = this.getRequest().getParameter("name");
		long id = StringUtil.parsLong(this.getRequest().getParameter("id"));

		AdminUser adminUser = new AdminUser();
		adminUser.setAdminName(name);
		adminUser.setAdminUserId(id);

		int result = adminUserService.updateAdminUser(adminUser);
		if (result > 0) {
			return SUCCESS;
		} else {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_UPDATE_ERROR);
			return ERROR;
		}
	}

	@Action(value = "pageAddAdminUser", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/addAdminUser.jsp") })
	public String pageAddAdminUser() {
		return SUCCESS;
	}

	@Action(value = "addAdminUser", results = {
			@Result(name = SUCCESS, location = "/html/administrator/system/adminUserList.jsp") })
	public String addAdminUser() {
		String name = this.getRequest().getParameter("name");
		String password = this.getRequest().getParameter("password");

		AdminUser adminUser = new AdminUser();
		adminUser.setAdminName(name);
		adminUser.setAdminPassword(password);
		adminUser.setStatus(1);

		int result = adminUserService.addAdminUser(adminUser);
		if (result > 0) {
			return SUCCESS;
		} else {
			this.getRequest().setAttribute("error", ErrorMessage.ADMIN_ADD_ERROR);
			return ERROR;
		}

	}
}
